// Sensor of Wireless Sensor Network applications..
// Mina Jung @ Syracuse University
// April, 2010

#import <random.h>
#import <objc/runtime.h>

#import "Sensor.h"


#ifdef USE_PIXMAP
static id <Pixmap> bugPixmap = nil;
#endif


@implementation Sensor


- setModelSwarm: s
{
  modelSwarm = s;
  sensorSpace = [modelSwarm getSensorSpace];

  age = 0;

  return self;
}

- setVision: (int)v
{
  vision = v;
 
  return self;
}

- setTimerFreq: (int)val
{
  timerFreq = val;
 
  return self;
}

- setDataType: (int)id
{
  sid = id;

  return self;
}


- step
{

printf("\nSTEP %d - AMT - %f", mid, amtBat);
  if(amtBat > 0)
  	[modelSwarm changeMoteBatteryCapID: mid Value: amtBat];

  // update Sensor information
  age++;

  if (amtBat <= 0 && moteState == Active)
    moteState = Die;

//  if (moteState == Active)
//    [self updateSensedValue];


  return self;
}


- updateSensedValue: (int) t
{
  int startx, starty, stopx, stopy;
  int sizex;
  int sizey; 
  int xlook, ylook, count = 0;

  int val;

  if(sid == -1)
    return self;

 

  SensedValue sumVal = 0;

  startx = x - vision;
  starty = y - vision;
  stopx = x + vision;
  stopy = y + vision;

  sizex = [sensorSpace getSizeX];
  sizey = [sensorSpace getSizeY];

  if (startx < 0)
    startx = 0;
  if (starty < 0)
    starty = 0;
  if (stopx >= sizex)
    stopx = sizex - 1;
  if (stopy >= sizey)
    stopy = sizey - 1;


  for (ylook = starty; ylook <= stopy; ylook++)
  {
    for (xlook = startx; xlook <= stopx; xlook++)
    {
      val = [sensorSpace getEnvironmentValueAtX: xlook Y: ylook A: t];

      if(val != -1)
      {
        sumVal += val;

        count++;
      }
    }
  }

  sensedValue[t-1] = (sumVal>0)?1:0;

  return self;
}

- (SensedValue)getSensedValue: (int) t
{
  return sensedValue[t-1];
}

- setSentMsgs: (int)num
{
  sentMsgsNow = num - sentMsgs;
  sentMsgs = num;
  return self;
}

- (int)getSentMsgs
{
  return sentMsgs;
}

- (int)getSentMsgsNow
{
  return sentMsgsNow;
}

- setRecvMsgs: (int)num
{
  recvMsgsNow = num - recvMsgs;
  recvMsgs = num;
  return self;
}

- (int)getRecvMsgs
{
  return recvMsgs;
}

- (int)getRecvMsgsNow
{
  return recvMsgsNow;
}

- (double)getAmtBattery
{
  return amtBat;
}

- setAmtBattery: (double)amt_bat
{
  amtBat = amt_bat;

  return self;
}


- (double)getInitBattery
{
  return initBat;
}

- setInitBattery: (double)amt_bat
{
  initBat = amt_bat;

  return self;
}

- (double)getMaxBattery
{
  return maxBat;
}

- setMaxBattery: (double)amt_bat
{
  maxBat = amt_bat;

  return self;
}

- setTotCurrentType: (int)type To: (double)value
{
  switch(type)
  {
  case 0:
    secCurCpu = value - totCurCpu;
    totCurCpu = value;
    break;
  case 1:
    secCurRadio = value - totCurRadio;
    totCurRadio = value;
    break;
  case 2:
    secCurLeds = value - totCurLeds;
    totCurLeds = value;
    break;
  }

  return self;
}

- (double)getSecCurrent
{
    return (secCurCpu + secCurRadio + secCurLeds);
}

- (double)getSecCurrentC
{
    return secCurCpu;
}

- (double)getSecCurrentR
{
    return secCurRadio;
}

- (double)getSecCurrentL
{
    return secCurLeds;
}

- (double)getTotCurrent
{
    return (totCurCpu + totCurRadio + totCurLeds);
}

- (double)getTotCurrentC
{
    return totCurCpu;
}

- (double)getTotCurrentR
{
    return totCurRadio;
}

- (double)getTotCurrentL
{
    return totCurLeds;
}

- (SensorState)getSensorState
{
  return moteState;
}

- setSensorState: (SensorState)state
{
  moteState = state;

  return self;
}

- setSensorId: (int)id
{
  mid = id;

  return self;
}

- setAppId: (int)id
{
  aid = id;

  return self;
}

- (int)getSensorId
{
  return mid;
}

- (int)getAppId
{
  return aid;
}

- (int)getX
{
  return x;
}

- (int)getY
{
  return y;
}

- setApplication: (char *)name
{
  strcpy(application, name);

printf("\n%s Application", application);

  return self;
}

- setTotalCurrent: (double)total
{
  totalCurrent = total;

  return self;
}

- (double)getTotalCurrent
{
  return totalCurrent;
}



- drawSelfOn: (id <Raster>)r
{
  [r drawPointX: x Y: y Color: moteState];

  return self;
}

@end
